﻿define(function (require) {
    var utility = require('utility/utility');
    var service = require('utility/service');
    require('utility/memberbox');

    return Backbone.View.extend({
        tagName: "div",
        template: _.template($("#message-container").html()),
        events: {
            "click .div-add": "chooseReceiver",
            "click .btn-back": "goBack",
            "click .action-button": "sendMessage"
        },
        render: function () {
            $(this.el).html(this.template({}));
            console.log("message container render");

            $('#message-page').append(this.el);
            $("#messageReceiver").memberbox({ width: $('#text-title').width() + 'px' });
            return this;
        },
        initialize: function () {
            $('#message-page').empty();
            _.bindAll(this, 'render');
        },
        chooseReceiver: function (e) {
            EventProxy.trigger(EventProxy.Events.chooseReceiver);
            EventProxy.trigger(EventProxy.Events.goToPage, "#contact-page");
        },
        goBack: function () {
            EventProxy.trigger(EventProxy.Events.goToPage, "#contact-page");
        },
        addMembers: function (members) {
            $("#messageReceiver").memberbox('addUsers', members.persons);
            $("#messageReceiver").memberbox('addGroups', members.groups);
        },
        sendMessage: function () {
            var memberString = $("#messageReceiver").memberbox('getIdList');
            var messageTitle = $('#txt-title').val();
            var messageText = $('#txt-content').val();

            //todo:判错
            if (memberString.length <= 0) {
                alert('选人啊！！！');
                return;
            }
            if (messageText.length <= 0) {
                alert('没内容啊！！！');
                return;
            }

            service.DoSend(function (e) {
                alert(e.Status);
            }, {
                memberString: memberString,
                messageTitle: messageTitle,
                messageText: messageText,
                needConfirm: false
            });

        },
        clear: function () {
            $(this.el).remove();
        }
    });

});